package com.msb.lmsc03;

import java.sql.*;

public class Book {
    private int id;
    private String name;
    private String author;
    private double price;

    private Connection conn;
    private ResultSet rs;
    private Statement stmt;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    //链接数据库getConnection()__start
    public Connection getConnection() {
        try {
            //加载数据库驱动--(准备电话)
            Class.forName("com.mysql.cj.jdbc.Driver");

            //通过DriverManger获取数据连接--（输电话号）
            String url = "jdbc:mysql://localhost:3306/sxs";

            String username = "root";

            String password = "123456";

            conn = DriverManager.getConnection(url, username, password);

            System.out.println("数据库连接成功！");
        } catch (Exception e) {

            e.printStackTrace();
            System.out.print("数据库连接失败！");
        }
        return conn;
    }

    //链接数据库getConnection()__end
    //回收数据库资源
    public void close(){
        if(rs!=null){
            try{
                rs.close();
            }catch(SQLException e){
                e.printStackTrace();
            }
            rs=null;
        }
        if(stmt != null){
            try{
                stmt.close();
            }catch(SQLException e){
                e.printStackTrace();
            }
            stmt=null;
        }
        if(conn!=null){
            try{
                conn.close();

            }catch(SQLException e){
                e.printStackTrace();
            }
            conn=null;
        }
    }
    //回收数据库资源close_end

}

